package com.manli.taobao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.manli.taobao.config.TaoBaoKeConfig;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.*;
import com.taobao.api.response.TbkDgMaterialOptionalResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 淘宝客接口
 * @author Shensg
 *
 * 2020年4月9日
 */
public class TaoBaoKeUtil {
	
	/**
	 * C:\Users\Administrator\.m2\repository\com\manlitech>mvn install:install-file -Df
		ile=taobaoke.jar -DgroupId=com.manlitech -DartifactId=taobaokesdk -Dversion=1.0
		-Dpackaging=jar
	 */
	
	public static void main(String[] args) throws ApiException {
		TaobaoClient client = new DefaultTaobaoClient("https://eco.taobao.com/router/rest",
				TaoBaoKeConfig.MANLITECHLIFE_APPKEY, TaoBaoKeConfig.MANLITECHLIFE_APPSECRET);
		/**
		 * taobao.tbk.sc.tpwd.convert( 淘宝客-服务商-淘口令解析&转链 )
		 */
		
		
		/*TbkScTpwdConvertRequest req = new TbkScTpwdConvertRequest();
		req.setPasswordContent("￥2k12308DjviP￥");
		req.setAdzoneId(TaoBaoKeConfig.MANLITECHLIFE_ADZONEID);
		req.setDx("1");
		req.setSiteId(TaoBaoKeConfig.MANLITECHLIFE_SITEID);
		String sessionKey = "610270942c244f58b19ad6bf0209bd4d428abf5c20146ec2207857006948";
		TbkScTpwdConvertResponse rsp = client.execute(req, sessionKey);
		System.out.println(rsp.getBody());*/
		
		/**
		 * ( 淘宝客-公用-淘口令生成 )
		 */
		/*TbkTpwdCreateRequest req = new TbkTpwdCreateRequest();
		req.setUserId(TaoBaoKeConfig.MANLI_USERID);//漫狸的USERID
		req.setText("长度大于5个字符");
		req.setUrl("https://uland.taobao.com/");
		req.setLogo("https://uland.taobao.com/");
		req.setExt("{}");
		TbkTpwdCreateResponse rsp = client.execute(req);
		System.out.println(rsp.getBody());*/

//		TbkContentGetRequest req = new TbkContentGetRequest();
//		req.setAdzoneId(TaoBaoKeConfig.MANLITECHLIFE_ADZONEID);
//		req.setType(1L);
//		req.setBeforeTimestamp(1590891326L);
//		req.setCount(10L);
//		req.setCid(2L);
//		req.setImageWidth(300L);
//		req.setImageHeight(300L);
//		req.setContentSet(1L);
//		TbkContentGetResponse rsp = client.execute(req);
//		System.out.println(rsp.getBody());

//
//        TbkDgMaterialOptionalRequest req = new TbkDgMaterialOptionalRequest();
//        req.setPageSize(20L);
//        req.setPageNo(1L);
//        req.setQ("牛奶");
//        req.setAdzoneId(TaoBaoKeConfig.MANLITECHLIFE_ADZONEID);
//        TbkDgMaterialOptionalResponse response = client.execute(req);
//		System.out.println(response.getBody());
//        System.out.println(response.getBody());

		try {
			System.out.println(searchGoodsImgs("牛奶"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 通过商品名查询淘宝客，商品信息，返回图片列表
	 * @param goodsName
	 * @return
	 * @throws Exception
	 */
	public static List<Object> searchGoodsImgs(String goodsName)throws Exception{
		TaobaoClient client = new DefaultTaobaoClient("https://eco.taobao.com/router/rest",
				TaoBaoKeConfig.MANLITECHLIFE_APPKEY, TaoBaoKeConfig.MANLITECHLIFE_APPSECRET);
		TbkDgMaterialOptionalRequest req = new TbkDgMaterialOptionalRequest();
		req.setPageSize(20L);
		req.setPageNo(1L);
		req.setQ(goodsName);
		req.setAdzoneId(TaoBaoKeConfig.MANLITECHLIFE_ADZONEID);
		TbkDgMaterialOptionalResponse response = client.execute(req);
		System.out.println(response.getBody());
		JSONObject object = JSONObject.parseObject(response.getBody());
		List<Object> list = new ArrayList<>();
		if(object.get("tbk_dg_material_optional_response") != null){
			JSONObject object1 = object.getJSONObject("tbk_dg_material_optional_response");
			if(object1.get("result_list") != null){
				JSONObject resultList = object1.getJSONObject("result_list");
				if(resultList.get("map_data") != null){
                    JSONArray object2 = resultList.getJSONArray("map_data");
                    if(object2.size() != 0){
                        for(int i=0;i<object2.size();i++){
                            JSONObject object3 = (JSONObject)object2.get(i);
                            if(object3.get("small_images") != null){
                                JSONObject object4 = object3.getJSONObject("small_images");
                                if(object4.get("string") != null){
                                    JSONArray images = object4.getJSONArray("string");
                                    list.addAll(images);
                                }
                            }
                        }
                    }
                }
			}
		}
		return list;
	}
}
